﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Scrabler;

using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using FireAnt.Windows;
using FireAnt.Windows.Forms.Util;

namespace ScrablerShell
{ 
     
    public partial class GUi : Form
    { 
       
        private void keypress(object sender, EventArgs e)
        {


            try
            {
                
                HotKey hotKey = sender as HotKey;
                if (hotKey != null)
                {

                    // force our window to the foreground
                    hotKey.ActivateWindow(Handle);
                    this.shohid();
               //     MessageBox.Show(hotKey.Key.ToString());

                    // TODO: WRITE COOL CODE HERE
                }

            }
            catch (Exception ex)
            {


                //Program.errorreporting(ex);
            }

        }
       public static  ScriptRepo repo = new ScriptRepo();
        ScrablerCore scarabler = new ScrablerCore();
     public  static  bool autohide = !false;
       public static  Keys shortcutkey;
        string[] defconst = {"keyshortcut","gui","securedirs" };
        string[] val = { "ScrablerShell.GUi.shortcutkey", "ScrablerShell.GUi", "ScrablerShell.GUi.securedirs" };
        const string settingsfile = "settings.cs";
        GlobalHotkeys hotkeys = new GlobalHotkeys();
       // ShortcutBox shortbox = new ShortcutBox();
        public static string securedirs;
        void ConfigureApp()
        {
            ScrablerCore.definedConstants = defconst;
            ScrablerCore.definedValues = val;
            //Keys[] ikeys = new Keys[1];
            
            scarabler.ReadScriptInLightMode(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), settingsfile));
          //  ikeys[0] = shortcutkey;
         //   this.tabConsoleControl1.IngoreKeys = ikeys;
            tabConsoleControl1.SecureDirs = GUi.securedirs;
            hotKey1.Key = shortcutkey;
            hotKey1.RegisterHotkey(FireAnt.Windows.Forms.Util.Modifiers.None, shortcutkey);
            hotKey1.HotKeyPressed += new EventHandler(this.keypress);
          


        }
     
        public GUi()
        {
            InitializeComponent();
            this.ConfigureApp();
            this.createScriptsDirectory(Environment.UserName);
            this.Autoexecutescripts(Environment.UserName);
            this.Text = Application.ProductName + " - " + Application.ProductVersion;
          //  this.Text += shortcutkey.ToString();
            
            
     
            
        }
        
        
        public void createScriptsDirectory(string username)
        {


            try
            {
                if (username != null)
                {
                    Program.FindScriptsFolder(username);
                    string path;
                    //  MessageBox.Show(Program.ScriptsFolder);
                    if (Program.ScriptsFolder != null)
                    {
                        if (Directory.Exists(Program.ScriptsFolder) == false)
                        {
                            // MessageBox.Show(Program.ScriptsFolder + "\\" + ScrablerCore.ScriptsFolder);
                            Directory.CreateDirectory(Program.ScriptsFolder);




                        }



                    }
                }
                // webBrowser.ScriptsFolder = Program.ScriptsFolder + "\\" + ScrablerCore.ScriptsFolder;


            }
            catch (Exception ex)
            {


                //Program.errorreporting(ex);
            }



        }
        public void Autoexecutescripts(string username)
        {
            try
            {
                if (username != null)
                {
                    string[] filelist = Directory.GetFiles(Program.ScriptsFolder);
                    scarabler.GetExeCutingApplicationVersion(Assembly.LoadFile(Application.ExecutablePath));

                    if (filelist != null)
                    {

                        // ScrablerCore.cp.ReferencedAssemblies.Add(Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\")) + "\\AxInterop.MOZILLACONTROLLib.dll");
                      ScrablerCore.cp.ReferencedAssemblies.Add(Application.ExecutablePath);
                        //  ScrablerCore.cp.ReferencedAssemblies.Add(Application.ExecutablePath);
                        ScrablerCore.cp.ReferencedAssemblies.Add(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Replace(@"file:\", "") + "\\Scrabler.dll");
                        //ScrablerCore.definedConstants = definedConstants;
                        //   ScrablerCore.definedValues = definedValues;
                        //Interop.MOZILLACONTROLLib.dll

                        foreach (string file in filelist)
                        {
                            if ((file != null) && (file.EndsWith("scrf") == true))
                            {

                                int i;
                                // ProcessHost prohost = new ProcessHost(); 
                                scarabler.ReadScriptAndInstall(file,repo.Binaryfolder);


                            }
                            

                        }
                        //autoastart.scrf


                    }



                }


            }
            catch (Exception ex)
            {


               // Program.errorreporting(ex);
            }




        }

        private void GUi_Load(object sender, EventArgs e)
        {
            notifyIcon1.Icon = this.Icon;
            notifyIcon1.Visible = true;
            notifyIcon1.Text = Application.ProductName;
        }

        private void newTabToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tabConsoleControl1.createNewTabSession();
        }

        private void GUi_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {

                if(GUi.shortcutkey== e.KeyCode)
                {

                    this.shohid();
                }
               
            }
            catch (Exception)
            {



            }
        }

        private void GUi_Resize(object sender, EventArgs e)
        {
            try
            {
                

           //  this.shohid();
               

            }
            catch (Exception)
            {



            }
        }

        private void newWindowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                GUi g = new GUi();
                g.Show();

            }
            catch (Exception)
            {



            }
            
        }
        void shohid()
        { 

            try
            {
              if (autohide == true)
                {

                    if (this.WindowState== FormWindowState.Minimized)
                    {

                        this.ShowInTaskbar = true;
                        //   this.MainMenuStrip.Items.Insert(this.FindToolStripMenuItem("βοήθειαToolStripMenuItem",this.MainMenuStrip), this.DefaultAdditionalCommandsMenu);
                        //this.Activate();
                        this.WindowState = FormWindowState.Normal;
                      this.Show();

                    }
                    else if ((this.WindowState == FormWindowState.Minimized) && (this.ShowInTaskbar == false)&&(this.Visible!=true ))
                    {
                        
                        this.ShowInTaskbar = !true;
                        //   this.MainMenuStrip.Items.Insert(this.FindToolStripMenuItem("βοήθειαToolStripMenuItem",this.MainMenuStrip), this.DefaultAdditionalCommandsMenu);

                        this.WindowState = FormWindowState.Minimized;
                        this.Hide();




                    }
                }


            }
            catch (Exception)
            {



            }

        }
        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            try
            {

                this.shohid();



            }
            catch (Exception)
            {



            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {

                this.shohid();



            }
            catch (Exception)
            {



            }
        }

        private void showHideToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {

                this.shohid();



            }
            catch (Exception)
            {



            }
        }

        private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            try
            {

                //this.shohid();



            }
            catch (Exception)
            {



            }
        }

        private void installScriptsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {

                ScrablerInstaller inst = new ScrablerInstaller();
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    inst.Install(openFileDialog1.FileName, Program.ScriptsFolder);
                    this.Autoexecutescripts(Environment.UserName);



                }



            }
            catch (Exception)
            {



            }
        }

        private void GUi_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {

                shohid();


            }
            catch (Exception)
            {



            }
        }

        private void clearTempToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {

             GUi.repo.ClearTempfolder();

            }
            catch (Exception)
            {



            }
        }

       

       
    }
}
